/**
* Handles loading and saving of files. 
*/
var FileManager = Class.create();
FileManager.prototype = {
	/**
	* @constructor
	* @param {String} annotationFileEditor reference to the AnnotationFileEditor-object.  
	*/
  initialize: function(annotationFileEditor) {
    this.annotationFileEditor = annotationFileEditor;
    this.javaFM = annotationFileEditor.toolApplet;
    this.xmlParser = FileManager.getXMLParser();
  },
  /**
  * @return {Array of Strings} Names of available top-level schemas
  */
  getSchemaList: function() {
    //return javaFM.getSchemaList();
  },
  /**
  * Load annotations file. 
  * @param {String} schemaName Name of the top-level schema of the message the annotationfile is for. 
  * @return {Node} root-node of annotationfile. 
  * @throws IOException 
  * @throws SecurityException 
  */
  getAnnotationFile: function(schemaName) {
	  var filepath = 'Annotations/'+schemaName+'.xml';
	  var xmlstr = this.javaFM.loadFile(filepath);
    if (xmlstr.substr(0,9) == "Exception") {
	    var err = xmlstr.substr(10);
	    if (err.startsWith('FileNotFound'))
	    	return document.createElement('annotations');
	    if (err.startsWith('SecurityException'))
		    this.annotationFileEditor.fatal(EditorErrors.securityError, xmlstr);
		  if (err.startsWith('IOException'))
		  	this.annotationFileEditor.fatal(EditorErrors.IOError, xmlstr);
  	}
  	var doc = this.xmlParser.parseFromString(xmlstr, 'text/xml');
   	if (doc.documentElement.tagName == "parserError") {
 	  	this.annotationFileEditor.printError("error in annotation file. Loading defaults");
 	  	return document.createElement('annotations');
   	}
   	return doc.documentElement;
  }
  
};

/*
* Return an object which can parse an xml-string to an xml-tree using the 
* interface of the W3C DOMParser.
* doc = getXMLParser().parseFromString(txt,'text/xml');
*/
FileManager.getXMLParser = function() {
	if (typeof DOMParser != 'undefined') {
		return new DOMParser();
	} else if (window.ActiveXObject) {
		var parser = new Object();
		parser.parseFromString = function (xmlstr, format) {
			var doc = new ActiveXObject('Microsoft.XMLDOM');
			doc.async = 'false';
			doc.loadXML(xmlstr);
			return doc;	
		};
		return parser;
	} else {
		this.annotationFileEditor.fatal('No DOM-parser in browser' +
		 EditorErrors.browserSupport());
	}
};